//
//  FavoriteViewController.m
//  YHYRealx
//
//  Created by yanghuiyan on 16/5/4.
//  Copyright © 2016年 yanghuiyan. All rights reserved.
//

#import "FavoriteViewController.h"
#import "HomePageDataModel.h"
@interface FavoriteViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) YHYBaseTableView *tableView;

@property (nonatomic, retain) NSMutableArray *dataSource;

@property (nonatomic, retain) UIImageView *emptyImageview;

@property (nonatomic, retain) MBProgressHUD *HUD;

@end

@implementation FavoriteViewController

- (void)dealloc {
    
    _tableView.delegate = nil;
    _tableView.dataSource = nil;
    
    [_tableView release];
    [_dataSource release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated {
    
    self.dataSource = [NSMutableArray array];
    self.dataSource = [[YHY_DataBaseTool shareDataBase] selectHomePageDataModelTable];
    
    if (self.dataSource.count == 0) {
        
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        self.emptyImageview =[[UIImageView alloc] initWithFrame:CGRectMake((WIDTH *20)/375, (HEIGHT *200)/667, (WIDTH *335)/375, (HEIGHT *335)/667)];
        
        self.emptyImageview.image = [UIImage imageNamed:@"10"];
        
        [self.view addSubview:self.emptyImageview];
        
        // 否则隐藏
    }else{
        
        self.emptyImageview.hidden = YES;
        
    }
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:1.00 green:0.71 blue:0.64 alpha:1.00];
    //自定义标题视图
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    titleLabel.text = @"收藏";
    titleLabel.font = [UIFont boldSystemFontOfSize:22];
    titleLabel.textColor = [UIColor grayColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    [titleLabel release];
    
    self.tableView = [[YHYBaseTableView alloc] initWithFrame:YHY];
    [self.tableView registerClass:[YHYBaseTableViewCell class] forCellReuseIdentifier:@"YHYBaseTableViewCellIdentifier"];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationAction:) name:@"reload" object:nil];

}

- (void)notificationAction:(NSNotification *)notification {
    
    [self.tableView reloadData];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YHYBaseTableViewCellIdentifier"];
    HomePageDataModel *model = self.dataSource[indexPath.row];
    cell.textLabel.text = model.hp_author;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}


#pragma mark - 删除
//重写系统提供按钮的方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    //首先让父类调用
    [super setEditing:editing animated:animated];
    //让tableView处于编辑状态下
    [self.tableView setEditing:YES animated:YES];
}

#pragma mark - 编辑tableView
//设置删除样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

//是否允许编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

//完成编辑
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"------------------------%ld--------------------", indexPath.row);
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        // 先移除数据 再删除行
        HomePageDataModel *songDetailModel = _dataSource[indexPath.row];
        [self.dataSource removeObjectAtIndex:indexPath.row];
        
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        
        [[YHY_DataBaseTool shareDataBase] deleteHomePageDataModelTable:[NSString stringWithFormat:@"%@", songDetailModel.hp_content]];
        
        self.dataSource = [[YHY_DataBaseTool shareDataBase] selectHomePageDataModelTable];
        
        if (self.dataSource.count == 0) {
            
            self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            
            //设置类型
            self.HUD.mode = MBProgressHUDModeText;
            
            //设置文字
            self.HUD.labelText = @"暂时没有收藏了┗|｀O′|┛ 嗷~~";
            
            [self.HUD hideAnimated:YES afterDelay:3];
            
            self.emptyImageview.hidden = NO;
            
        }
        
        
        
        [self.tableView reloadData];
        
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HomePageBeforeListDetailVC *vc = [[HomePageBeforeListDetailVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    
    vc.dataModel = self.dataSource[indexPath.row];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
